package com.deer.wms.inventory.web;

import com.deer.wms.intercept.annotation.User;
import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.inventory.constant.InventoryConstant;
import com.deer.wms.inventory.model.Inventory.InventoryTransact;
import com.deer.wms.inventory.model.InventoryAdjustDetail;
import com.deer.wms.inventory.model.InventoryAdjustDetailCriteria;
import com.deer.wms.inventory.model.InventoryAdjustDetailDto;
import com.deer.wms.inventory.service.InventoryAdjustDetailService;
import com.deer.wms.inventory.service.InventoryTransactService;
import com.deer.wms.project.root.core.result.Result;
import com.deer.wms.project.root.core.result.ResultGenerator;
import com.deer.wms.project.root.util.DateUtils;
import com.deer.wms.review.manage.model.BillRecord;
import com.deer.wms.review.manage.model.BillRecordTypeConstants;
import com.deer.wms.review.manage.service.BillRecordService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
* Created by ll on 2020/03/12.
*/
@RestController
@RequestMapping("/inventory/adjust/details")
public class InventoryAdjustDetailController {

    @Autowired
    private InventoryAdjustDetailService inventoryAdjustDetailService;
    @Autowired
    private InventoryTransactService inventoryTransactService;
    @Autowired
    private BillRecordService billRecordService;

    @PostMapping
    public Result add(@RequestBody InventoryAdjustDetail inventoryAdjustDetail) {
        inventoryAdjustDetailService.save(inventoryAdjustDetail);
        return ResultGenerator.genSuccessResult();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        inventoryAdjustDetailService.removeById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PutMapping
    public Result update(@RequestBody InventoryAdjustDetail inventoryAdjustDetail) {
        inventoryAdjustDetailService.updateById(inventoryAdjustDetail);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/{id}")
    public Result detail(@PathVariable Integer id) {
        InventoryAdjustDetail inventoryAdjustDetail = inventoryAdjustDetailService.getById(id);
        return ResultGenerator.genSuccessResult(inventoryAdjustDetail);
    }

    @GetMapping
    public Result list(InventoryAdjustDetailCriteria criteria) {
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<InventoryAdjustDetail> list = inventoryAdjustDetailService.list();
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @GetMapping("/findList")
    public Result findList(InventoryAdjustDetailCriteria criteria) {
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<InventoryAdjustDetailDto> list = inventoryAdjustDetailService.findList(criteria);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @ApiOperation(value = "调整确认", notes = "调整确认")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class,  required = true)
    })
    @PostMapping("/adjustAffirm")
    @Transactional
    public Result adjustAffirm(@RequestBody InventoryAdjustDetailCriteria criteria, @ApiIgnore @User CurrentUser currentUser) {
        List<InventoryAdjustDetailDto> inventoryAdjustDetailDtos = inventoryAdjustDetailService.findList(criteria);
        for(InventoryAdjustDetailDto inventoryAdjustDetailDto:inventoryAdjustDetailDtos) {
            if(inventoryAdjustDetailDto.getState().equals(1)) {
                InventoryTransact inventoryTransact = new InventoryTransact(InventoryConstant.INVENTORY_ADJUST, null,
                        inventoryAdjustDetailDto.getDetailNo(), inventoryAdjustDetailDto.getReason(), 1, currentUser.getUserId(),
                        currentUser.getUserName(), DateUtils.getNowDateTimeString(), inventoryAdjustDetailDto.getItemCode(),
                        inventoryAdjustDetailDto.getItemName(), inventoryAdjustDetailDto.getWareId(), inventoryAdjustDetailDto.getWareName(),
                        inventoryAdjustDetailDto.getOrganizationId(), inventoryAdjustDetailDto.getOrganizationName(),
                        inventoryAdjustDetailDto.getBatchId(), inventoryAdjustDetailDto.getBatchName(), inventoryAdjustDetailDto.getBoxCode(),
                        inventoryAdjustDetailDto.getCellCode(), inventoryAdjustDetailDto.getPackDescribe(),
                        inventoryAdjustDetailDto.getPackDetailId(), inventoryAdjustDetailDto.getQuantity(), inventoryAdjustDetailDto.getOrganizationId(),
                        inventoryAdjustDetailDto.getOrganizationName(), inventoryAdjustDetailDto.getBatchId(), inventoryAdjustDetailDto.getBatchName(),
                        inventoryAdjustDetailDto.getBoxCode(), inventoryAdjustDetailDto.getCellCode(), inventoryAdjustDetailDto.getPackDetailId(),
                        inventoryAdjustDetailDto.getPackDescribe(), inventoryAdjustDetailDto.getAdjustQuantity(), inventoryAdjustDetailDto.getWareId(),
                        inventoryAdjustDetailDto.getWareName());
                inventoryTransact.setFromTransRatio(inventoryAdjustDetailDto.getTransRatio());
                inventoryTransact.setToTransRatio(inventoryAdjustDetailDto.getTransRatio());
                inventoryTransactService.run(inventoryTransact);
                inventoryAdjustDetailDto.setState(2);
                inventoryAdjustDetailDto.setAdjustTime(DateUtils.getNowDateTimeString());
                inventoryAdjustDetailDto.setAdjustUserId(currentUser.getUserId());
                inventoryAdjustDetailDto.setAdjustUserName(currentUser.getUserName());
                inventoryAdjustDetailService.updateById(inventoryAdjustDetailDto);
                BillRecord billRecord = new BillRecord(inventoryAdjustDetailDto.getBillNo(), DateUtils.getNowDateTimeString(),
                        currentUser.getUserId(), currentUser.getUserName());
                billRecord.setRecordType(BillRecordTypeConstants.INVENTORY_ADJUST);
                billRecord.setMemo("库存调整");
                billRecordService.save(billRecord);
            }
        }
        return ResultGenerator.genSuccessResult();
    }

}
